﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using RazorEngine;


namespace _14期Razor引擎
{
    public class RazorHelper
    {
        public static void Render(string ModelName, object obj, params IncludeType[] includeTypes)
        {
            
            //进行compile
            if (includeTypes.Length>0)
            {
                int len = includeTypes.Length;
                for (int i = 0; i < len; i++)
                {
                    IncludeType t1 = includeTypes[i];
                    if (t1.IsFile)
                    {
                        string temp= File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "views\\" + t1.Content);
                        Razor.Compile(temp,t1.Title);
                    }
                    else
                    {
                        Razor.Compile(t1.Content,t1.Title);

                    }
                }
            }

            //执行Parse
            string str1 = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "views\\" + ModelName);
            string htmlstr= Razor.Parse(str1, obj);

            //扔到输出流响应
            HttpContext.Current.Response.Write(htmlstr);
        }
        public class IncludeType
        {
            public string Title { get; set; }
            public string Content { get; set; }
            public bool IsFile { get; set; }//true表示文件,false表示字符串
        }

    }
}